package com.peam.web.util;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.GeneratePresignedUrlRequest;
import com.aliyun.oss.model.OSSObject;
import com.peam.common.utils.uuid.IdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import java.util.Date;
@Component
public class OssUtil  {
    @Autowired
    private  OSSClient ossClient;
    static  String bucketName = "peam";
    /**
     * 上传文件到阿里云OSS
     *
     * @param file     要上传的文件
     * @param folder   文件存储的目录
     * @return 文件在OSS中的URL
     */
    public  String uploadFile(MultipartFile file, String folder) throws IOException {
        String originalFilename = file.getOriginalFilename();
        if (originalFilename == null || originalFilename.isEmpty()) {
            // 文件名为空，处理异常情况，这里可以返回错误信息或抛出异常
            throw new IllegalArgumentException("上传文件名为空");
        }

        // 获取文件后缀名
        String fileExtension = "";
        int dotIndex = originalFilename.lastIndexOf(".");
        if (dotIndex > 0 && dotIndex < originalFilename.length() - 1) {
            fileExtension = originalFilename.substring(dotIndex);
        }

        String simpleUUID = IdUtils.simpleUUID();
        String fileName = simpleUUID + fileExtension; // 使用"ceshi"作为文件名称前缀，并加上后缀名
        String key = folder + "/" + fileName;
        ossClient.putObject(bucketName, key, file.getInputStream());
        Date expiration = new Date(System.currentTimeMillis() + 3600 * 1000); // 设置URL过期时间为1小时
        GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucketName, key);
        request.setExpiration(expiration);
        URL url = ossClient.generatePresignedUrl(request);
        return url.toString();
    }
    /**
     * 查看文件在OSS中的URL
     *
     * @return 文件在OSS中的URL
     */
    public  String getFileUrl(String url1) {
        String key =url1;
        Date expiration = new Date(System.currentTimeMillis() + 3600 * 1000); // 设置URL过期时间为1小时
        GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucketName, key);
        request.setExpiration(expiration);
        URL url = ossClient.generatePresignedUrl(request);
        return url.toString();
    }
    /**
     * 查看文件在OSS中的URL
     *
     * @param folder   文件存储的目录
     * @param fileName 文件名称
     * @return 文件在OSS中的URL
     */
    public  String getFileUrl(String folder, String fileName) {
        String key = folder + "/" + fileName;
        Date expiration = new Date(System.currentTimeMillis() + 3600 * 1000); // 设置URL过期时间为1小时
        GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucketName, key);
        request.setExpiration(expiration);
        URL url = ossClient.generatePresignedUrl(request);
        return url.toString();
    }

    /**
     * 下载文件
     *
     * @param folder   文件存储的目录
     * @param fileName 文件名称
     * @return 文件流
     */
    public InputStream downloadFile(String folder, String fileName) {
        String key = folder + "/" + fileName;
        OSSObject object = ossClient.getObject(bucketName, key);
        return object.getObjectContent();
    }


}
